import 'package:blocks_blkk/components/return_button.dart';
import 'package:blocks_blkk/components/steer.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:yundongjia_ble/yundongjia_ble.dart';
import 'package:flutter/material.dart';

import '../app_configure.dart';
import 'device_data_context.dart';
import 'entity/function_enum.dart';
import 'function_page.dart';

/*
 * 遥控界面 2.0
 * FunctionEntry -> RomotePage
 */
class RemotePage20 extends FunctionPage {
  RemotePage20({super.key});

  @override
  _RemotePage20State createState() => _RemotePage20State(FunctionType.Remote);
}

class _RemotePage20State extends FunctionPageState {
  _RemotePage20State(FunctionType functionType)
      : super(functionType: functionType);

  // int _leftMeter = 0;
  // int _rightMeter = 0;
  // int _cValue = 0;
  int _dValue = 0;
  // bool _lockStatus = AppConfig.instance.controlLockStatus == 0;
  // bool _eStatus = false;
  // bool _fStatus = false;

  List<String> btnImageNames = [
    'assets/seires_1_level_1.png',
    'assets/seires_1_level_2.png',
    'assets/seires_1_level_3.png',
  ];

  // List<GestureTapCallback> _btnFunction = [];

  // void settingBtnFunction() {
  //   //setState(() {});
  // }

  // void switchBtnFunction() {
  //   // double temp = _steerPosition;
  //   // setState(() {
  //   //   _steerPosition = _steerElse;
  //   //   _steerElse = temp;
  //   // });
  // }

  double _steerPosition = ScreenUtil().setHeight(150);
  double _steerElse = 0;
  @override
  void initState() {
    super.initState();
    // _btnFunction.add(settingBtnFunction);
    // _btnFunction.add(switchBtnFunction);
    // _btnFunction.add(switchBtnFunction);
  }

  @override
  void dispose() {
    YundongjiaBle.instance.stop();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    debugPrint('====== remote V2 build');

    return background(
      <Widget>[
        ReturnButton(),
        bluetootnBtn(shape: BoxShape.rectangle),
        _steer(),
        // _slider(),
        channelA(),
        channelD(),
      ],
      useBackground: false,
    );
  }

  Widget channelA() {
    return channelBtn(
      right: ScreenUtil().setHeight(150.0),
      top: ScreenUtil().setHeight(60.0),
      width: ScreenUtil().setHeight(80.0),
      height: ScreenUtil().setHeight(80.0),
      imagename: (AppConfig.instance.channelASpeed == 1
          ? 'assets/seires_1_a_on.png'
          : 'assets/seires_1_a_off.png'),
      ontap: () {
        setState(() {
          AppConfig.instance.channelASpeed =
              AppConfig.instance.channelASpeed * -1;
        });
      },
    );
  }

  Widget channelB() {
    return channelBtn(
      right: ScreenUtil().setHeight(240.0),
      top: ScreenUtil().setHeight(60.0),
      width: ScreenUtil().setHeight(80.0),
      height: ScreenUtil().setHeight(80.0),
      imagename: (AppConfig.instance.channelBSpeed == 1
          ? 'assets/seires_1_b_on.png'
          : 'assets/seires_1_b_off.png'),
      ontap: () {
        setState(() {
          AppConfig.instance.channelBSpeed =
              AppConfig.instance.channelBSpeed * -1;
        });
      },
    );
  }

  Widget channelC() {
    return channelBtn(
      right: ScreenUtil().setHeight(130.0),
      top: ScreenUtil().setHeight(60.0),
      width: ScreenUtil().setHeight(80.0),
      height: ScreenUtil().setHeight(80.0),
      imagename: (AppConfig.instance.channelCSpeed == 1
          ? 'assets/seires_1_c_on.png'
          : 'assets/seires_1_c_off.png'),
      ontap: () {
        setState(() {
          AppConfig.instance.channelCSpeed =
              AppConfig.instance.channelCSpeed * -1;
        });
      },
    );
  }

  Widget channelD() {
    return channelBtn(
      right: ScreenUtil().setHeight(50.0),
      top: ScreenUtil().setHeight(60.0),
      width: ScreenUtil().setHeight(80.0),
      height: ScreenUtil().setHeight(80.0),
      imagename: (AppConfig.instance.channelDSpeed == 1
          ? 'assets/seires_1_d_on.png'
          : 'assets/seires_1_d_off.png'),
      ontap: () {
        setState(() {
          AppConfig.instance.channelDSpeed =
              AppConfig.instance.channelDSpeed * -1;
        });
      },
    );
  }

  Widget _leftSteer(final backgroundImg, final controlImg) {
    return Steer(
      left: _steerPosition,
      right: _steerElse,
      bottom: ScreenUtil().setHeight(150),
      width: ScreenUtil().setHeight(380.0),
      height: ScreenUtil().setHeight(380.0),
      radio: 0.35,
      changeValue: setVertical,
      direction: SteerMoveEnum.Vertical,
      backgroundImg: 'assets/$backgroundImg',
      controlImg: 'assets/$controlImg',
    );
  }

  Widget _rightSteer(final backgroundImg, final controlImg) {
    return Steer(
      left: _steerElse,
      right: _steerPosition,
      bottom: ScreenUtil().setHeight(150),
      width: ScreenUtil().setHeight(380.0),
      height: ScreenUtil().setHeight(380.0),
      radio: 0.35,
      changeValue: setHorizontal,
      direction: SteerMoveEnum.Horizontal,
      backgroundImg: 'assets/$backgroundImg',
      controlImg: 'assets/$controlImg',
    );
  }

  //档位容器
  // Widget _levelContainer(final double _width, final double _height,
  //     {required double right, required double top}) {
  //   double __right = right;
  //   double __top = top;
  //   double __padding = 10;
  //   return Positioned(
  //     right: __right,
  //     top: __top,
  //     width: _width,
  //     height: _height,
  //     child: Container(
  //       decoration: const BoxDecoration(
  //         borderRadius: BorderRadius.all(Radius.circular(4.0)),
  //         image: DecorationImage(
  //             image: AssetImage('assets/seires_1_level_bg.png'),
  //             fit: BoxFit.fill),
  //       ),
  //       width: _width,
  //       height: _height,
  //       padding: EdgeInsets.all(__padding),
  //       child: Column(
  //         children: [
  //           _levelBtn(0, _width - 2 * __padding, _width - 2 * __padding),
  //           Expanded(child: Container()),
  //           _levelBtn(1, _width - 2 * __padding, _width - 2 * __padding),
  //           Expanded(child: Container()),
  //           _levelBtn(2, _width - 2 * __padding, _width - 2 * __padding),
  //         ],
  //       ),
  //     ),
  //   );
  // }

  // Widget _levelBtn(final int _level, double width, double height) {
  //   return GestureDetector(
  //     onTap: _btnFunction[_level],
  //     child: Container(
  //       decoration: const BoxDecoration(
  //         borderRadius: BorderRadius.all(Radius.circular(4.0)),
  //       ),
  //       width: width,
  //       height: height,
  //       padding: EdgeInsets.all(ScreenUtil().setHeight(10)),
  //       child: Image(
  //         image: AssetImage(btnImageNames[_level]),
  //       ),
  //     ),
  //   );
  // }

  @override
  void setVertical({required double x, required double y}) {
    final adjustY = y * AppConfig.instance.channelASpeed;
    // print('setVertical  y = $y  ');
    final int yval = (adjustY > 0.3
        ? 0x80 + (((adjustY - 0.3) / 0.7) * 24).toInt()
        : adjustY < -0.3
            ? (((-0.3 - adjustY) / 0.7) * 24).toInt()
            : 0x80);

    YundongjiaBle.instance.sendAdvertiseCommand(
        DeviceDataContext.instance.devcie.getDataAt(4, yval));
  }

  @override
  void setHorizontal({required double x, required double y}) {
    //离圆心偏移距离大于0.3才计算，小于0.3 则发送0
    final double adjustX = x * AppConfig.instance.channelDSpeed;
    final int xval = (adjustX > 0.3
        ? (((adjustX - 0.3) / 0.7) * 24).toInt()
        : adjustX < -0.3
            ? 0x80 - (((0.3 + adjustX) / 0.7) * 24).toInt()
            : 0x80);
    _dValue = xval;

    setHorizontalCommand();
  }

  void setHorizontalCommand() {
    //print('x = $xval  y = $y')
    YundongjiaBle.instance.sendAdvertiseCommand(
        DeviceDataContext.instance.devcie.getDataAt(7, _dValue));
  }

  //设置按钮事件处理
  // @override
  // void onSettingTap() {
  //   // Navigator.of(context).push(
  //   //   MaterialPageRoute(builder: (context) {
  //   //     return RemoteSetting30Page();
  //   //   }),
  //   // );
  // }

  // Widget _slider() {
  //   return Positioned(
  //     right: ScreenUtil().setHeight(500.0),
  //     left: ScreenUtil().setHeight(500.0),
  //     top: ScreenUtil().setHeight(100.0),
  //     height: ScreenUtil().setHeight(80.0),
  //     child: Container(
  //       child: Row(
  //         children: [
  //           Expanded(child: Container()),
  //           _sliderLeft(),
  //           Expanded(child: Container()),
  //           _sliderRight(),
  //           Expanded(child: Container()),
  //         ],
  //       ),
  //     ),
  //   );
  // }

  Widget _steer() {
    return Positioned(
      right: ScreenUtil().setHeight(10.0),
      left: ScreenUtil().setHeight(10.0),
      top: ScreenUtil().setHeight(150.0),
      bottom: ScreenUtil().setHeight(150.0),
      child: Row(
        children: [
          Expanded(child: Container()),
          _leftSteer(
              'seires_m_steer_left.png', 'seires_m_steer_thumb_left.png'),
          Expanded(flex: 2, child: Container()),
          _rightSteer(
              'seires_m_steer_right.png', 'seires_m_steer_thumb_right.png'),
          Expanded(child: Container()),
        ],
      ),
    );
  }

  // Widget _settings() {
  //   return Positioned(
  //     right: ScreenUtil().setHeight(80.0),
  //     left: ScreenUtil().setHeight(80.0),
  //     bottom: ScreenUtil().setHeight(120.0),
  //     height: ScreenUtil().setHeight(100.0),
  //     child: Container(
  //       child: Row(
  //         children: [
  //           _settingBtn(),
  //           Expanded(child: Container()),
  //           // _lockBtn(),
  //         ],
  //       ),
  //     ),
  //   );
  // }

  //指示表
  // Widget _settingBtn() {
  //   return GestureDetector(
  //     onTap: onSettingTap,
  //     child: Container(
  //       height: ScreenUtil().setHeight(100.0),
  //       width: ScreenUtil().setHeight(100.0),
  //       decoration: const BoxDecoration(
  //         image: DecorationImage(
  //           image: AssetImage('assets/seires_1_setting.png'),
  //         ),
  //       ),
  //     ),
  //   );
  // }

  //指示表
  // Widget _lockBtn() {
  //   return GestureDetector(
  //     onTap: onLockBtn,
  //     child: Container(
  //       height: ScreenUtil().setHeight(100.0),
  //       width: ScreenUtil().setHeight(100.0),
  //       decoration: BoxDecoration(
  //         image: DecorationImage(
  //           image: AssetImage(_lockStatus
  //               ? 'assets/seires_1_lock.png'
  //               : 'assets/seires_1_lock_on.png'),
  //         ),
  //       ),
  //     ),
  //   );
  // }

  // Widget _sliderLeft() {
  //   return SliderSelf(
  //     width: ScreenUtil().setHeight(120),
  //     height: ScreenUtil().setHeight(80),
  //     controlImg: 'assets/switch_thumb.png',
  //     direction: SliderTypeEnum.Horizontal,
  //     max: 2,
  //     value: 1,
  //     changeValue: (v) {
  //       if (v == 0xFF) {
  //         _eStatus = true;
  //       } else {
  //         _eStatus = false;
  //       }
  //     },
  //   );
  // }

  // Widget _sliderRight() {
  //   return SliderSelf(
  //     width: ScreenUtil().setHeight(120),
  //     height: ScreenUtil().setHeight(80),
  //     controlImg: 'assets/switch_thumb.png',
  //     direction: SliderTypeEnum.Horizontal,
  //     max: 2,
  //     value: 1,
  //     changeValue: (v) {
  //       if (v == 0xFF) {
  //         _fStatus = true;
  //       } else {
  //         _fStatus = false;
  //       }
  //     },
  //   );
  // }

  // @override
  // void onLockBtn() {
  //   setState(() {
  //     _lockStatus = !_lockStatus;
  //   });

  //   AppConfig.instance.controlLockStatus = (_lockStatus ? 0 : 1);
  //   // bus.emit('SteerReset', 'null');
  // }
}
